//
//  IMBaseCell.m
//  ChatMainDemo
//
//  Created by peiw on 16/1/21.
//  Copyright © 2016年 peiw. All rights reserved.
//

#import "IMBaseCell.h"
#import "IMChatDefines.h"

@implementation IMBaseCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    self.avatarView = [UIImageView new];
    self.avatarView.userInteractionEnabled = YES;
    self.avatarView.contentMode = UIViewContentModeScaleAspectFill;
    self.avatarView.clipsToBounds = YES;
    self.avatarView.layer.cornerRadius = AvatarSize / 2;
    [self.contentView addSubview:self.avatarView];
    
    switch (self.chatType) {
        case Single:
        {
            self.nameLabel = [UILabel new];
            self.nameLabel.backgroundColor = ClearColor;
            self.nameLabel.font = ChatNameFont;
            self.nameLabel.textColor = ChatNameTextColor;
            [self.contentView addSubview:self.nameLabel];
            self.nameLabel.frame = CGRectMake(CGRectGetMaxX(self.avatarView.frame)+13, 0, ChatCellNameWidth, ChatCellNameHeight);
            
        }
            break;
        case Group:
        case Bulk:
            break;
        default:
            break;
    }
    
    self.bubbleView = [IMCellBubbleView new];
    [self.contentView addSubview:self.bubbleView];
    
    self.activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    self.activityView.hidesWhenStopped  =YES;
    self.activityView.hidden = YES;
    [self.contentView addSubview:self.activityView];
    
    self.contentLabel = [YYLabel new];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.font = ChatContentFont;
    [self.contentView addSubview:self.contentLabel];
    
}

- (void)setContent:(IMCellDisplayData *)cellDisplay {
    id<IMChatCellProtocol> cell = (id<IMChatCellProtocol>)self;
    CGSize size = [cell sizeForContent:cellDisplay];
    UIImage *bubleImage = [cellDisplay bubbleImage];
    IMCellConfig *config = [cellDisplay chatCellConfig];
    bubleImage = [bubleImage stretchableImageWithLeftCapWidth:config.insets.left topCapHeight:config.insets.top];
    self.bubbleView.image = bubleImage;
    switch (cellDisplay.direction) {
        case Send:
            self.avatarView.frame = CGRectMake(IMAvatarEdge, 0, AvatarSize, AvatarSize);
            self.bubbleView.frame = CGRectMake(CGRectGetMaxX(self.avatarView.frame), CGRectGetMaxY(self.nameLabel.frame), size.width + IMBubbleContentGap, size.height);
            break;
        case Recv:
            self.avatarView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - IMAvatarEdge - AvatarSize, 0, AvatarSize, AvatarSize);
            self.bubbleView.frame = CGRectMake(CGRectGetMinX(self.avatarView.frame)- IMAvatarBubbleGap - size.width - IMBubbleContentGap, CGRectGetMaxY(self.nameLabel.frame), size.width + IMBubbleContentGap, size.height);
            break;
        default:
            break;
    }
    
    //test
    self.avatarView.image = [UIImage imageNamed:@"profile"];
}

//子类实现
- (CGFloat)cellHeightForMessage:(IMCellDisplayData *)cellDisplay {
    return 0;
}

@end

@implementation IMCellBubbleView

@end